package com.oem.android.activity.user;

import static com.oem.android.contant.URLConstant.GATEWAY_URL;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.oem.android.R;
import com.oem.android.activity.file.FileChooserActivity;
import com.oem.android.config.RetrofitClient;
import com.oem.android.model.user.LoginFormDTO;
import com.oem.android.model.Result;
import com.oem.android.service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class LoginActivity extends AppCompatActivity {

    public static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        EditText username = findViewById(R.id.username);
        EditText password = findViewById(R.id.password);
        Button loginButton = findViewById(R.id.loginButton);
        TextView registerPrompt = findViewById(R.id.register_prompt);

        loginButton.setOnClickListener(view -> {
            String userInput = username.getText().toString().trim();
            String passInput = password.getText().toString().trim();

            Log.i(TAG, "用户名为：" + userInput);
            Log.i(TAG, "密码为：" + passInput);

            if (userInput.isEmpty() || passInput.isEmpty()) {
                Toast.makeText(LoginActivity.this, "Please enter all fields", Toast.LENGTH_SHORT).show();
                return;
            }

            // 调用登录 API
            login(userInput, passInput);
        });

        registerPrompt.setOnClickListener(view -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });
    }

    private void login(String userInput, String passInput) {
        Retrofit retrofit = RetrofitClient.getClient(GATEWAY_URL);
        UserService userService = retrofit.create(UserService.class);

        LoginFormDTO loginFormDTO = new LoginFormDTO(userInput, passInput);

        userService.loginWithUsername(loginFormDTO).enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                if (response.isSuccessful() && response.body() != null) {
                    Result result = response.body();
                    if ("1".equals(result.getCode())) {
                        // 登录成功
                        Intent intent = new Intent(LoginActivity.this, FileChooserActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Toast.makeText(LoginActivity.this, "Network error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}